package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.UserDataSource;
import com.ruoyi.system.service.UserDataSourceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



import java.util.List;

@Controller
@RequestMapping("/system/userDataSource")
public class SysUserDataSourceController extends BaseController {
    @Autowired
    private UserDataSourceService userDataSourceService;

    private String prefix = "system/userDataSource";


    @RequiresPermissions("system:userDataSource:view")
    @GetMapping()
    public String modelType()
    {
        return prefix + "/userDataSource";
    }

    /**
     * 根据条件查询用户数据源
     * @param userDataSource
     * @return
     */
    @RequiresPermissions("system:userDataSource:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserDataSource userDataSource)
    {
        startPage();
        List<UserDataSource> userDataSources = userDataSourceService.selectDataSourceAll(userDataSource);
        return getDataTable(userDataSources);
    }

//-------------------------------
    /**
     * 新增用户数据源
     */
    @RequiresPermissions("system:userDataSource:add")
    @Log(title = "用户数据源", businessType = BusinessType.INSERT)
    @GetMapping("/add")
    public String add(ModelMap mp)
    {
        // 取身份信息
//        SysUser user = ShiroUtils.getSysUser();
//        mp.put("user",user);
        return prefix + "/add";
    }

    /**
     * 新增用户数据源提交
     * @param userDataSource
     * @return
     */
    @RequiresPermissions("system:userDataSource:add")
    @Log(title = "用户数据源", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated UserDataSource userDataSource)
        {

        userDataSource.setCreateId(ShiroUtils.getSysUser().getUserId().toString());
        userDataSource.setCreateName(ShiroUtils.getSysUser().getLoginName());
        //用户ID和创建人ID相同
        userDataSource.setUserId(Integer.valueOf(ShiroUtils.getSysUser().getUserId().toString()));
        return toAjax(userDataSourceService.insertDataSourceInfo(userDataSource));
    }

//-------------------------------

    /**
     * 根据ids删除用户数据源
     * @param ids
     * @return
     */
    @RequiresPermissions("system:userDataSource:remove")
    @Log(title = "用户数据源", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        return toAjax(userDataSourceService.deleteDataSourceByIds(Convert.toStrArray(ids)));
    }


    /**
     * 检查用户数据源名称是否重复
     */
    @PostMapping("/checkDataSourceNameUnique")
    @ResponseBody
    public Boolean checkDataSourceNameUnique(@Validated UserDataSource userDataSource)
    {
        return userDataSourceService.checkDataSourceNameUnique(userDataSource);
    }


    /**
     * 修改用户数据源
     */
    @RequiresPermissions("system:userDataSource:edit")
    @Log(title = "用户数据源", businessType = BusinessType.UPDATE)
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user",user);
        UserDataSource u = new UserDataSource();
        u.setId(Integer.valueOf(id));
        List<UserDataSource> userDataSources = userDataSourceService.selectDataSourceAll(u);
        mmap.put("userDataSource",userDataSources.get(0));
        return prefix + "/edit";
    }

    /**
     * 用户数据源更新
     */
    @RequiresPermissions("system:userDataSource:edit")
    @Log(title = "用户数据源", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@Validated UserDataSource userDataSource)
    {

        //添加用户信息
        userDataSource.setUpdateId(ShiroUtils.getSysUser().getUserId().toString());
        userDataSource.setUpdateName(ShiroUtils.getSysUser().getLoginName());
        return toAjax(userDataSourceService.updateDataSourceById(userDataSource));
    }





}
